﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Common;
using GroupChat2013.Interface;
using GroupChat2013.Logic;
using GroupChat2013.Logic.Interface;
using GroupChat2013.Service;
using GroupChat2013.Entity;

namespace GroupChat2013.Main.Components
{
    /// <summary>
    /// 新的群分享 通知
    /// </summary>
    public class GroupShareReceiveProcess : IReceiveProcess
    {
        /// <summary>
        /// 消息处理标志
        /// </summary>
        private string key;

        private string storeFileName = "storeFileName";

        private string shareFromGroupId = "shareFromGroupId";

        private FrmInformation frmInformation;

        private string InfoFormat = "【{1}】: 群内用户 {2} 分享了文件《{3}》\n【{0}】";

        /// <summary>
        /// 构造函数
        /// </summary>
        public GroupShareReceiveProcess(FrmInformation frmInformation)
        {
            this.frmInformation = frmInformation;
            key = ConstClass.GETGROUPSHAREFILECREATEDNOTIFY;
        }

        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Process(Entity.Protocol.TransferMsgInfo message)
        {
            if (!message.action.Equals(key))
                return false;

            string fileName = message.data[storeFileName];
            string fromGroupId = message.data[shareFromGroupId];

            GroupShareInfo info = ServiceUrlProxy.GetInstance().GetGroupShareFile(fromGroupId, fileName, EngineContext.Current.CurrUserInfo.CID);

            string content = string.Format(InfoFormat, info.fileShareTime, info.sharefileGroupName, info.sharefileUserName, info.shareFilename);
            InformationItem item = new InformationItem()
            {
                Url = info.groupSharefileListUri,
                Content = content
            };

            if (frmInformation == null || frmInformation.IsDisposed)
                frmInformation = new FrmInformation();

            frmInformation.Show();
            frmInformation.AddInformationItem(item);
             
            return true;
        }
    }
}
